package com.laputacastle.dr.mapper;

import com.laputacastle.dr.model.DrCatalog;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Created by Mybatis Generator 2019/11/29
*/
@Repository
public interface DrCatalogMapper {
    @Delete({
        "delete from t_dr_catalog",
        "where c_id = #{cId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String cId);

    @Insert({
        "insert into t_dr_catalog (c_id, c_pid, ",
        "c_name, c_imgurl, ",
        "n_valid)",
        "values (#{cId,jdbcType=VARCHAR}, #{cPid,jdbcType=VARCHAR}, ",
        "#{cName,jdbcType=VARCHAR}, #{cImgurl,jdbcType=VARCHAR}, ",
        "#{nValid,jdbcType=INTEGER})"
    })
    int insert(DrCatalog record);

    @InsertProvider(type=DrCatalogSqlProvider.class, method="insertSelective")
    int insertSelective(DrCatalog record);

    @Select({
        "select",
        "c_id, c_pid, c_name, c_imgurl, n_valid",
        "from t_dr_catalog",
        "where c_id = #{cId,jdbcType=VARCHAR}"
    })
    @Results({
        @Result(column="c_id", property="cId", jdbcType=JdbcType.VARCHAR, id=true),
        @Result(column="c_pid", property="cPid", jdbcType=JdbcType.VARCHAR),
        @Result(column="c_name", property="cName", jdbcType=JdbcType.VARCHAR),
        @Result(column="c_imgurl", property="cImgurl", jdbcType=JdbcType.VARCHAR),
        @Result(column="n_valid", property="nValid", jdbcType=JdbcType.INTEGER)
    })
    DrCatalog selectByPrimaryKey(String cId);

    @UpdateProvider(type=DrCatalogSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(DrCatalog record);

    @Update({
        "update t_dr_catalog",
        "set c_pid = #{cPid,jdbcType=VARCHAR},",
          "c_name = #{cName,jdbcType=VARCHAR},",
          "c_imgurl = #{cImgurl,jdbcType=VARCHAR},",
          "n_valid = #{nValid,jdbcType=INTEGER}",
        "where c_id = #{cId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(DrCatalog record);

    @Select("SELECT c_id,\n" +
            "c_name,\n" +
            "c_menu_name,\n" +
            "c_pid,\n" +
            "c_imgurl,\n" +
            "1 AS DEPTH\n" +
            "FROM t_dr_catalog where n_valid = 1")
    @Results({
            @Result(property = "cId", column = "c_id"),
            @Result(property = "cPid", column = "c_pid"),
            @Result(property = "cName", column = "c_name"),
            @Result(property = "cMenuName", column = "c_menu_name"),
            @Result(property = "cImgurl", column = "c_imgurl"),
            @Result(property = "depth", column = "depth")
    })
    List<DrCatalog> getCatalogTreeData();
}